﻿using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CadDotNetTools
{
    public static class DimStyleTools
    {
        /// <summary>
        /// 创建一个新的标注样式
        /// </summary>
        /// <param name="db">Database</param>
        /// <param name="styleName">标注样式名</param>
        /// <returns></returns>
        public static ObjectId AddDimStyle(this Database db, string styleName)
        {
            //打开标注样式表
            var dst = db.DimStyleTableId.GetObject(OpenMode.ForRead) as DimStyleTable;
            //如果不存在名为StyleName的标注样式，则新建标注样式
            if (!dst.Has(styleName))
            {
                //定义一个新的标注样式表记录
                var dstr = new DimStyleTableRecord();
                dstr.Name = styleName;  //设置标注样式名

                dst.UpgradeOpen();    //切换标注样式表的状态为写以添加新的标注样式
                dst.Add(dstr);  //将标注样式表记录的信息添加到标注样式表中
                //吧标注样式表记录添加到事物处理中
                db.TransactionManager.AddNewlyCreatedDBObject(dstr, true);
                dstr.DowngradeOpen();    //为了安全，将标注样式表的状态切换为读
            }
            return dst[styleName];         //返回新添加的标注样式表记录的ObjectId
        }
    }
}
